<!-- 申请信用卡 -->
<template>
    <div id="applyCard">
        <div class="banner">
            <van-swipe class="my-swipe" :autoplay="3000" :height="130" indicator-color="white">
                <van-swipe-item v-for="(item,i) in bannerList" :key="i">
                    <img :src="item.imgUrl" alt="">
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="hot">
            <img src="@/assets/applyCard/hotIcon.png" alt="">
            <div>热门任务</div>
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list
                v-model:loading="loading"
                :finished="finished"
                finished-text="没有更多了"
                @load="onLoad"
            >
                <div class="itemWrap" v-if="list.length > 0">
                    <div class="item" v-for="item in list" :key="item" :title="item" @click="goDetail(item)">
                        <div class="title">{{item.title}}</div>
                        <div class="tag">{{item.author}}</div>
                        <div class="contText">{{item.content}}</div>
                        <!-- <van-divider /> -->
                        <!-- <div class="itemBtm">奖励￥{{item.likeCount}}</div> -->
                    </div>
                    <div class="item1" v-if="list.length % 3 != 0"></div>
                </div>
                <van-empty v-else description="暂无数据" />
            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script>
import { ref, reactive, getCurrentInstance } from 'vue';
import { Swipe, SwipeItem , List , PullRefresh , Divider , Empty } from 'vant';
import { ACCESS_TOKEN , BRANDID } from '@/utils/types';
import storage from 'store';
export default {
    components:{
        [Swipe.name]:Swipe,
        [SwipeItem.name]:SwipeItem,
        [List.name]:List,
        [PullRefresh.name]:PullRefresh,
        [Divider.name]:Divider,
        [Empty.name]:Empty,
    },
    setup() {
        console.log('setup')
        const { proxy } = getCurrentInstance();
        // const phone = proxy.getHashParam('phone') // 推荐人手机号
        // const brandId = proxy.getHashParam('brandId');
        // 获取系统的token
        storage.clearAll();
        const tokenKey = proxy.getHashParam('tokenKey');
        const getToken = () => {
            proxy.$http.brandApi.getRequestParams(tokenKey)
            .then(res=>{
                console.log(res,'res')
                if(res.success){
                    storage.set(ACCESS_TOKEN,res.data)
                    getBanner();
                    finished.value = false;
                }else{
                    proxy.$toast({position: 'bottom',message:res.errMessage})
                }
            })
        }
        getToken()
        const bannerList = ref([]); // 轮播图数据
        // 信用卡数据
        const list = ref([]);
        const loading = ref(false);
        const finished = ref(true);
        const refreshing = ref(false);
        const page = ref(0);
        const size = ref(10);
        // 下拉刷新，上拉加载更多
        const onRefresh = () => {
            // 清空列表数据
            finished.value = false;
            // 重新加载数据
            // 将 loading 设置为 true，表示处于加载状态
            loading.value = true;
            page.value = 0;
            list.value = [];
            getData();
        };
        const onLoad = () => {
            getData();
        };
        // onRefresh();

        // 获取轮播图数据
        function getBanner(){
            proxy.$http.brandApi.queryAppInforMtion({infoType: 16,page : 0, size: 10})
            .then(res=>{
                console.log(res,'res')
                if(res.success){
                    bannerList.value = res.data.content;
                }else{
                    proxy.$toast({position: 'bottom',message:res.errMessage})
                }
            })
        }
        

        // 获取信用卡列表数据
        function getData(){
            proxy.$http.brandApi.queryAppInforMtion({infoType: 17,page : page.value, size: size.value})
            .then(res=>{
                console.log(res,'res')
                refreshing.value = false;
                if(res.success){
                    if(page.value == 0){
                        list.value = res.data.content;
                    }else{
                        list.value = list.value.concat(res.data.content);
                    }
                    page.value++;
                    loading.value = false;
                    console.log(list.value)
                    if(( page.value * size.value) >= res.data.totalElements){
                        finished.value = true;
                    }
                    console.log(loading.value);
                }else{
                    proxy.$toast({position: 'bottom',message:res.errMessage})
                }
            })
        }

        // 跳转详情页
        function goDetail(item){
            console.log(item)
            proxy.$router.push({name: 'applyCardDetail',params: { 'item' : JSON.stringify(item) }})
        }

        return {
            bannerList,
            onRefresh,
            onLoad,
            list,
            loading,
            finished,
            refreshing,
            goDetail,
        }
    }
}

</script>
<style scoped>
#applyCard{
    padding : 15px 16px;
    background: #FAFAFA;
    min-height: 100vh;
}
.banner{
    height: 130px;
}
.banner img{
    width: 100%;
}

.hot{
    display: flex;
    align-items: flex-end;
    font-size: 17px;
    font-weight: 500;
    color: #1A1A1A;
    margin: 10px 0;
}
.hot img{
    width: 26px;
    margin-right: 5px;
}

.itemWrap{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.item{
    width: 109px;
    height: 120px;
    box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.06);
    border-radius: 4px;
    margin: 5px 2px;
}
.title{
    padding: 0 7px;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    margin-top: 10px;
    font-size: 15px;
    font-weight: 500;
    color: #1F1F1F;
}
.tag{
    width: 60px;
    height: 18px;
    border-radius: 40px;
    background: #EBF2FC;
    color: #3B81F0;
    font-size: 10px;
    text-align: center;
    line-height: 18px;
    margin: 10px auto;
}
.contText{
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    color: #999999;
    font-size: 11px;
    padding: 0 7px 0px;
    height: 34px;
    line-height: 17px;
}
.van-divider{
    margin-bottom: 2px;
    margin-top: 10px;
}
.itemBtm{
    width: 92px;
    height: 20px;
    background: url('../../assets/applyCard/tagBg.png') no-repeat;
    background-size: 100% ;
    margin: 0 auto;
    padding-top: 12px;
    text-align: center;
    font-size: 11px;
    color: #FFFFFF;
}
.item1{
    width: 109px;
    height: 152px;
}
</style>